package com.example.service;

import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.entity.MedicationLogs;
import com.example.entity.Medications;
import com.example.entity.User;
import com.example.mapper.MedicationLogsMapper;
import com.example.mapper.MedicationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MedicationService {
    @Resource
    private MedicationMapper medicationMapper;
    @Resource
    private MedicationLogsMapper medicationLogsMapper;
    public Result addMedication(List<Medications> medicationsList) {
        for (Medications medications : medicationsList){
            if(!medicationMapper.addMedications(medications)){
                return Result.error("插入失败");
            }
        }
        return Result.success("插入成功");
    }

    public Result listMedication(User user) {
        // 获取当前日期的年月日部分
        LocalDate today = LocalDate.now();
        // 将LocalDate转换为Date对象（时间部分为00:00:00）
        Date date = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        List<JSONObject> medicationsList = medicationMapper.list(user.getUserId());
        List<MedicationLogs> medicationLogsList = medicationLogsMapper.listAll();
        medicationLogsList = medicationLogsList.stream()
                .filter(x -> x.getUserId().equals(user.getUserId()))
                .filter(x -> x.getLogDate().equals(date))
                .collect(Collectors.toList());
        Map<Integer,List<MedicationLogs>> map = medicationLogsList.stream().collect(Collectors.groupingBy(x -> x.getMedId()));
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (JSONObject medications : medicationsList){
            if(map.get(Integer.parseInt(medications.get("med_id").toString())) == null){
                medications.put("amount", 0);
            }else {
                medications.put("amount", map.get(Integer.parseInt(medications.get("med_id").toString())).get(0).getTaken());
            }
            jsonObjectList.add(medications);
        }
        return Result.success(jsonObjectList);
    }

    public Result deleteMedication(Medications medications) {
        if(!medicationMapper.deleteMedication(medications)){
            return Result.error("删除失败");
        }
        MedicationLogs medicationLogs = new MedicationLogs();
        medicationLogs.setMedId(medications.getMedId());
        medicationLogs.setUserId(medications.getUserId());
        if(!medicationLogsMapper.delete(medicationLogs)){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    public Result updateMedication(Medications medications) {
        if(!medicationMapper.updateMedicaiton(medications)){
            return Result.error("更新失败");
        }
        return Result.success("更新成功");
    }

    public Result listAll() {
        List<Medications> medicationsList = medicationMapper.listAll();
        return Result.success(medicationsList);
    }
}
